package sgi.client;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;

import sgi.message.Message;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.io.IOException;

public class SGIClientUI extends JFrame {

	private JPanel contentPane;
	private JTextField serveradd;
	private JTextField port;
	JTextArea notification;
	JTextArea fromserver;
	SGIClient client;

	/**
	 * Create the frame.
	 */
	public SGIClientUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setTitle("SGI Client");
		contentPane = new JPanel();
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		JLabel lblServerAddress = new JLabel("Server Address:");
		lblServerAddress.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblServerAddress.setBounds(38, 29, 105, 26);
		contentPane.add(lblServerAddress);
		
		JLabel lblPort = new JLabel("Port:");
		lblPort.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPort.setBounds(38, 61, 51, 17);
		contentPane.add(lblPort);
		
		serveradd = new JTextField();
		serveradd.setBounds(153, 34, 124, 20);
		contentPane.add(serveradd);
		serveradd.setColumns(15);
		
		port = new JTextField();
		port.setBounds(153, 61, 124, 20);
		contentPane.add(port);
		port.setColumns(10);
		
		JButton btnConnect = new JButton("Connect");
		btnConnect.setBounds(314, 60, 89, 23);
		btnConnect.setToolTipText("Connect to server");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				client.setHost(serveradd.getText());
				client.setPort(Integer.valueOf(port.getText()));
				 try {
					 	client.openConnection();
					 	setNotification("Connection to server succeeded.");
					 	client.handleMessageFromClientUI(new Message(Message.DISPLAY,"Client connected."));
				 } catch (IOException e) {setNotification("Connection to server failed."); }
			}
		});
		contentPane.add(btnConnect);
		
		JButton btnRead = new JButton("Read");
		btnRead.setToolTipText("");
		btnRead.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				fromserver.setText(null);
				Message msg1 = new Message(Message.SELECT,"select info from datatable");
				try {
						client.handleMessageFromClientUI(msg1);
						notification.setText("DB read succeeded.");
				} catch (IOException e1) {notification.setText("DB Read failed.");}
			}
		});
		btnRead.setBounds(27, 156, 89, 23);
		contentPane.add(btnRead);
		
		JButton btnWrite = new JButton("Write");
		btnWrite.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fromserver.setText(null);
				Message msg2 = new Message(Message.INSERT,"insert into datatable (info) values ('B')");
				try {
					   client.handleMessageFromClientUI(msg2);
					   notification.setText("DB write succeeded.");
				} catch (IOException e1) {notification.setText("DB write failed.");}
				
			}
		});
		btnWrite.setBounds(27, 190, 89, 23);
		contentPane.add(btnWrite);
		
		fromserver = new JTextArea();
		fromserver.setBounds(134, 156, 238, 57);
		fromserver.setEditable(false);
		contentPane.add(fromserver);
		
		notification = new JTextArea();
		notification.setForeground(new Color(255, 0, 0));
		notification.setBounds(38, 100, 365, 15);
		notification.setEditable(false);
		contentPane.add(notification);
	}
	
	public void setClient(SGIClient client)
	{
		this.client = client;
	}
	
	public void setNotification(String s)
	{
		notification.setText(s);
	}
	
	public void displayFromServer(String s)
	{
		fromserver.append(s + " ");
	}
	
	  public static void main(String[] args) throws IOException {
			
		SGIClientUI clientui = new SGIClientUI();
		clientui.setVisible(true);
		SGIClient client = new SGIClient(clientui);
		clientui.setClient(client);
	  }
}
	